package com.itheima;

import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.util.List;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/14 11:20
 */
@SpringBootTest
public class WordPOIDemo01 {


    @Test
    public void test01()throws  Exception{
        //创建操作word文档poi的对象
        XWPFDocument document = new XWPFDocument(new FileInputStream("D:\\practice\\Java报表数据可视化教程\\day03\\day03" +
                "\\资料\\test.docx"));

        //读取正文
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        for (XWPFParagraph paragraph : paragraphs) { //获取所有段落
            System.out.println(paragraph.getText()); //读取一段
            List<XWPFRun> runs = paragraph.getRuns(); //获取每一个片段
            for (XWPFRun run : runs) { //每一个片段的内容
                System.out.println(run.getText(0));
            }
        }

        //读取表格
        XWPFTable xwpfTable = document.getTables().get(0);
        List<XWPFTableRow> rows = xwpfTable.getRows();
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> tableCells = row.getTableCells();
            for (XWPFTableCell tableCell : tableCells) {
                List<XWPFParagraph> paragraphs1 = tableCell.getParagraphs();
                for (XWPFParagraph xwpfParagraph : paragraphs1) {
                    System.out.println(xwpfParagraph.getText());
                }
            }
        }
    }
}
